using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Mpi.Common.Controls
{
    public partial class BusyCtrl : Control
    {
        ProgressBar pb = new ProgressBar();
        private SpinningProgress _spinner;

        public SpinningProgress SpinProgress
        {
            get { return _spinner; }
            set { _spinner = value; }
        }

        private bool _busy;

        public bool Busy
        {
            get { return _busy; }
            set
            {
                _busy = value;
                if (_busy)
                {
                    this.pb.Style = ProgressBarStyle.Marquee;
                    this.Visible = true;
                    this.BringToFront();
                }
                else
                {
                    this.pb.Style = ProgressBarStyle.Blocks;
                    this.Visible = false;
                    this.SendToBack();
                }
            }
        }

        private Label _displayText;

        public string Text
        {
            get { return _displayText.Text ; }
            set { _displayText.Text = value; /*SetPosition();*/ }
        }

        public BusyCtrl()
        {
            InitializeComponent();
            base.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.OptimizedDoubleBuffer |
        ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
            base.UpdateStyles();

            this.Dock = DockStyle.Fill;
            BackColor = Color.FromArgb(120, 0, 0, 0);

            //pb.Width = 200;
            //pb.Style = ProgressBarStyle.Marquee;

            //SetPosition();
            //this.Controls.Add(pb);

            _spinner = new SpinningProgress();
            _spinner.Width = 24;
            _spinner.Height = 24;
            _spinner.Left = 100;
            _spinner.Top = 100;
            _spinner.BackColor = Color.Transparent;
            _spinner.InactiveSegmentColour = Color.FromArgb(20, 0, 0, 0);
            _spinner.ActiveSegmentColour = Color.Gainsboro;
            _spinner.TransistionSegmentColour = Color.White;
            _spinner.TransistionSegment = 3;
            
            this.Controls.Add(_spinner);

            _displayText = new Label();
            _displayText.BackColor = Color.Transparent;
            _displayText.ForeColor = Color.WhiteSmoke;
            _displayText.Text = "";
            _displayText.AutoEllipsis = true;
            _displayText.AutoSize = false;
            _displayText.Anchor = AnchorStyles.Left | AnchorStyles.Right;
            _displayText.Width = this.Width;
            _displayText.Height = 30;
            _displayText.TextAlign = ContentAlignment.MiddleCenter;

            this.Controls.Add(_displayText);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            SetPosition();
        }

        private void SetPosition()
        {
            _spinner.Top = (this.Height / 2) - (_spinner.Height / 2);
            _spinner.Left = (this.Width / 2) - (_spinner.Width / 2);

            _displayText.Left = 0;
            _displayText.Top = _spinner.Top - _displayText.Height - 5;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            // Calling the base class OnPaint
            base.OnPaint(pe);
        }
    }
}
